package dao;
import java.util.List;

import excepciones.DaoException;
import modelo.Usuario;

/**
 * Usuario DAO: Maintains Persistence between model and database
 * Based on Hibernate JPA
 */
public class UsuarioDao extends AbstractDao
{

   /**
    * Class Constructor [Empty]
    */
   public UsuarioDao()
   {
      super();
   }
   
   /**
    * Class Constructor
    * @param model   The Usuario model to manage persistence for
    */
   public UsuarioDao(Usuario UsuarioModel)
   {
      super(UsuarioModel);      
      this.model = (Usuario) model;
   }
   
   /**
    * Get attached Usuario model
    * @return model   A Usuario Model
    */
   @Override
   public Usuario getModel()
   {
      return (Usuario) model;
   }
   
   /**
    * Find Usuario Record by Id
    * Overrides Base Doa to convert to Usuario model
    *
    * @return model   A Usuario model found
    * @throws DaoException
    */
   @Override
   public Usuario findById(Long id) throws DaoException
   {
      try
      {
         model = (Usuario) super.findById(Usuario.class, id);
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
      
      
      return (Usuario) model;
   }
   
   /**
    * Find a Usuario by (unique) email address
    *
    * @param email   Email Address String
    * @return model   Usuario Model
    * @throws DaoException
    */
   public Usuario encontrarPorUsuario(String usr) throws DaoException
   {
      try
      {
         model = (Usuario) super.getEntityManager().
                 createQuery("Select u from Usuario as u where u.usuario = ?1")
                  .setParameter(1, usr)
                  .getSingleResult();
         //super.setModel( UsuarioModel );
         
      }
      catch(IllegalStateException e)
      {
         throw new DaoException("Find by Email Failed: " + e.getMessage());
      }
      catch(IllegalArgumentException e)
      {
         throw new DaoException("Find by Email Failed: " + e.getMessage());
      }
      catch(Exception e)
      {
         throw new DaoException("Find by Email Failed: " + e.getMessage());
      }
      
      
      return (Usuario) model;
   }
   
	public List<Usuario> getTodosLosEvaluadores() throws DaoException {
		try
	    {				         
	       @SuppressWarnings("unchecked")
		List <Usuario> usuarios = 
	      		 (List<Usuario>) super.getEntityManager().
	             createQuery("SELECT u FROM Usuario AS u JOIN u.perfiles AS p WHERE p.nombre = 'Evaluador'")              
	              .getResultList();
	       //super.setModel( UsuarioModel );
	       return usuarios;
			
	    }
	    catch(IllegalStateException e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	    catch(IllegalArgumentException e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	    catch(Exception e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	}
	
	public List<Usuario> getTodosLosUsuarios() throws DaoException {
		try
	    {				         
	       @SuppressWarnings("unchecked")
		List <Usuario> usuarios = 
	      		 (List<Usuario>) super.getEntityManager().
	             createQuery("Select u from Usuario as u")              
	              .getResultList();
	       //super.setModel( UsuarioModel );
	       return usuarios;	
	    }
	    catch(IllegalStateException e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	    catch(IllegalArgumentException e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	    catch(Exception e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	}
	
	public List<Usuario> getTodosLosIntegrantes() throws DaoException {
		try
	    {				         
	       @SuppressWarnings("unchecked")
		List <Usuario> usuarios = 
	      		 (List<Usuario>) super.getEntityManager().
	             createQuery("SELECT u FROM Usuario AS u JOIN u.perfiles AS p WHERE p.nombre = 'Integrante'")              
	              .getResultList();
	       //super.setModel( UsuarioModel );
	       return usuarios;
			
	    }
	    catch(IllegalStateException e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	    catch(IllegalArgumentException e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	    catch(Exception e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	}

}
